﻿using System;
using System.Windows.Input;


namespace MVVMS
{
    /// <summary>
    /// Simple Implementation of the ICommand Interface
    /// Use this in your ViewModels to expose commands to
    /// your view.
    /// </summary>
    public class ViewModelCommand : ICommand
    {
        private readonly Action execute;
        private readonly Func<bool> canExecute;

        public ViewModelCommand(Action execute, Func<bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public ViewModelCommand(Action execute)
        {
            this.execute = execute;
            this.canExecute = () => { return true; };
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute();
        }

        public event EventHandler CanExecuteChanged;

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, EventArgs.Empty);
        }

        public void Execute(object parameter)
        {
            execute();
        }
    }
}
